package com.hongning.railway.ui.main.fragments.college;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.hongning.railway.R;
import com.hongning.railway.databinding.FragmentCollegeLearnBinding;
import com.hongning.railway.ui.adapter.BaseFragmentPagerAdapter;
import com.hongning.railway.util.SoftKeyboardUtils;

import java.util.ArrayList;
import java.util.Arrays;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Author: zhangjing
 * DATA:2019-08-10 15:05
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class CollegeLearnFragment extends BaseFragment<FragmentCollegeLearnBinding,CollegeLearnViewModel> {
    ArrayList<Fragment> fragments=new ArrayList<>();
    public static CollegeLearnFragment newInstance() {

        Bundle args = new Bundle();

        CollegeLearnFragment fragment = new CollegeLearnFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_college_learn;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initData() {
        super.initData();
        initViewPager();
        binding.search.findViewById(R.id.iv_select_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startContainerActivity(CollegeFilterFragment.class.getCanonicalName());
            }
        });
        binding.search.findViewById(R.id.iv_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startContainerActivity(CollegeFilterFragment.class.getCanonicalName());
            }
        });
        binding.search.<EditText>findViewById(R.id.et_search).setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    SoftKeyboardUtils.hideSoftKeyboard(getActivity());
                    int pos = binding.vpContent.getCurrentItem();
                    if (pos ==0){
                        ((LearnUnCompleteFragment)fragments.get(0)).setloadByKeyord(v.getText().toString());
                    }else {
                        ((LearnCompleteFragment)fragments.get(1)).setloadByKeyord(v.getText().toString());
                    }
                }
                return true;
            }
        });
    }

    private void initViewPager() {
        String[] title={"未完成","已完成"};
        fragments.add(LearnUnCompleteFragment.newInstance());
        fragments.add(LearnCompleteFragment.newInstance());
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), fragments, Arrays.asList(title));
        binding.vpContent.setAdapter(pagerAdapter);
        binding.stlLearn.setViewPager(binding.vpContent,title);
    }
}
